///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_BBOX_PROXIES_H_
#define _BUOLA_BBOX_PROXIES_H_

#include <buola/buola.h>
#include <buola/bbox/protocol/cpeerconnection.h>
#include <buola/bbox/detail/cinputbase.h>
#include <buola/traits/signature.h>

namespace buola { namespace bbox {

class BElementProxy;
    
class BServiceProxy
{
    BServiceProxy(const protocol::PPeerConnection &pPeer,uint64_t pUID)
        :   mPeer(pPeer)
        ,   mUID(pUID)
    {}

public:
    template<typename tSignature,typename... tArgs>
    OSignatureReturn<tSignature> Call(const std::string &pName,const tArgs&... pArgs) const
    {
        return mPeer->CallMethod<OSignatureReturn<tSignature>>(mUID,pName,pArgs...);
    }
    
    BElementProxy operator[](const std::string &pName);

private:
    protocol::PPeerConnection mPeer;
    uint64_t mUID;

    friend class CSession;
    friend class BElementProxy;
};
    
BServiceProxy get_service(const std::string &pAddress);

class BElementProxy
{
    BElementProxy(const BServiceProxy &pService,const std::string &pName)
        :   mService(pService)
        ,   mName(pName)
    {}

public:
    void Connect(detail::CInputBase &pIn) const
    {
        std::pair<uint64_t,uint64_t> lIDs=mService.mPeer->ConnectTerminal(mService.mUID,mName);
        pIn.Connect(mService.mPeer,lIDs);
    }
    
private:
    BServiceProxy mService;
    std::string mName;

    friend class BServiceProxy;
};

inline BElementProxy BServiceProxy::operator[](const std::string &pName)
{
    return {*this,pName};
}
    
inline void link(const BElementProxy &pOut,detail::CInputBase &pIn)
{
    pOut.Connect(pIn);
}

/*namespace bbox*/ } /*namespace buola*/ }

#endif
